package com.xbdun.service.bmp.facade.open;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.xbdun.dao.bmp.dto.open.SysOrgFiledsDto;
import com.xbdun.dao.bmp.entity.XbdunSysOrgDo;
import com.xbdun.dao.bmp.entity.XbdunSysOrgFiledsDo;
import com.xbdun.dao.bmp.mapper.OrgUserMapper;
import com.xbdun.dao.bmp.mapper.SysOrgMapper;
import com.xbdun.facade.bmp.request.open.CachOrgInfoRequest;
import com.xbdun.facade.bmp.response.open.CachOrgInfoResponse;
import com.xbdun.facade.bmp.response.open.CardOrgInfoResponse;
import com.xbdun.facade.bmp.response.open.SearchJoinsBusiOpenResponse;
import com.xbdun.facade.bmp.service.open.OrgInfoOpenFacade;
import com.xbdun.service.bmp.service.cache.OrgInfoCacheService;
import com.xbdun.service.bmp.service.system.OrgUserService;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class OrgInfoOpenFacadeImpl implements OrgInfoOpenFacade {
	@Autowired
	OrgUserService orgUserService;
	@Autowired
	SysOrgMapper sysOrgMapper;
	@Autowired
	OrgUserMapper orgUserMapper;
	@Autowired
	OrgInfoCacheService orgInfoCacheService;
	public Map<Long,Integer> searchJoinsBusiOpen(String userCode,String wxUnionId){
		try{
			 return orgUserService.joinOrgCodes(userCode,wxUnionId);
		}catch(Exception e){
			log.error("查询已经关注企业，入参：用户编号[%s],用户UnionId[%s]，异常：{}",userCode,wxUnionId,e);
		}
		return null;
	}
	
	public List<SearchJoinsBusiOpenResponse> getOrgInfoList(List<Long> orgCodes) {
		List<SearchJoinsBusiOpenResponse> list = new ArrayList<SearchJoinsBusiOpenResponse>();
		if(CollectionUtils.isNotEmpty(orgCodes)){
			SearchJoinsBusiOpenResponse info = null;
			XbdunSysOrgDo sinlgeOrgInfo = null;
			XbdunSysOrgFiledsDo orgFildes = null;
			for(int i=0;i<orgCodes.size();i++){
				long long1 = NumberUtils.toLong(orgCodes.get(i)+"");
				if(long1>0){
					try{sinlgeOrgInfo = orgInfoCacheService.getSinlgeOrgInfo(long1);}catch(Exception e){}
					if(null!=sinlgeOrgInfo){
						try{orgFildes = orgInfoCacheService.getOrgFildes(long1);}catch(Exception e){}
						info = new SearchJoinsBusiOpenResponse();
						
						info.setLogoUrl(sinlgeOrgInfo.getLogo());
						info.setOrgCode(sinlgeOrgInfo.getOrgCode()+"");
						info.setOrgName(sinlgeOrgInfo.getOrgName());
						info.setRemark((null!=orgFildes?orgFildes.getMainCamp():""));//主营业务
						list.add(info);
						sinlgeOrgInfo = null;
						if(null!=orgFildes)orgFildes = null;
					}
				}
				
			}
			orgCodes.clear();
			orgCodes = null;
		}
		return list;
	}
	
	public CardOrgInfoResponse getCardOrgInfo(String orgCode, String userCode) {
		CardOrgInfoResponse result = null;
		long userCodex = NumberUtils.toLong(userCode);
		try{
			SysOrgFiledsDto dto = orgInfoCacheService.getOrgFildes(NumberUtils.toLong(orgCode));
			if(null!=dto){
				result = new CardOrgInfoResponse();
				BeanUtils.copyProperties(result, dto);
				//用户编号不为空，那么说明是机构用户
				if(userCodex>0){
					List<String> orgUserDepartRole = orgUserMapper.getOrgUserDepartRole(userCodex,Long.valueOf(orgCode));
					if(CollectionUtils.isNotEmpty(orgUserDepartRole)){
						result.setDepart(CollectionUtils.join(orgUserDepartRole, ","));	
						orgUserDepartRole.clear();
					}
				}
			}
		}catch(Exception e){
			log.error("根据卡片获取机构信息,机构编号["+orgCode+"],用户编号["+userCode+"]",e);
		}
		return result;
	}

	public CachOrgInfoResponse getCachOrgInfo(CachOrgInfoRequest request) {
		CachOrgInfoResponse respon = null;
		XbdunSysOrgDo sinlgeOrgInfo = null;
		try{
			if(request.getOrgCode()<=0)return respon;
			try{sinlgeOrgInfo = orgInfoCacheService.getSinlgeOrgInfo(request.getOrgCode());}catch(Exception e){}
			if(null!=sinlgeOrgInfo){
				respon = new CachOrgInfoResponse();
				respon.setOrgPhone(sinlgeOrgInfo.getOrgMobile());//电话
				respon.setOrgTel(sinlgeOrgInfo.getOrgLandline());//座机电话
				respon.setOrgAddr(sinlgeOrgInfo.getAddr());//企业地址
				respon.setOrglogo(sinlgeOrgInfo.getLogo());//企业LOGO
				respon.setOrgName(sinlgeOrgInfo.getOrgName());//机构名称
				sinlgeOrgInfo = null;
			}
		}catch(Exception e){
			log.error("从缓存中获取机构信息");
		}finally{
			if(null!=sinlgeOrgInfo)sinlgeOrgInfo = null;
		}
		return respon;
	}
}
